package com.example.androidcoursedesing.main.tab;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidcoursedesing.R;
import com.example.androidcoursedesing.main.adapter.SolarTermAdapter;
import com.example.androidcoursedesing.main.bean.SolarTermResponse;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class Fragment1 extends Fragment {
   private RecyclerView mRecyclerView;
   private Context mContext;
   private  SolarTermAdapter adapter;
   private List<SolarTermResponse.ListBean> list;

   private OkHttpClient client;

   private Handler handler = new Handler(Looper.myLooper()){
       @Override
       public void handleMessage(@NonNull Message msg) {
           super.handleMessage(msg);
           switch (msg.what){
               case 100:
                   SolarTermResponse response = (SolarTermResponse) msg.obj;
                   list = response.getList();
                   adapter.setData(list);
                   adapter.notifyDataSetChanged();
                   break;
               case  101:
                   SolarTermResponse response2 = (SolarTermResponse) msg.obj;
                   Toast.makeText(mContext, response2.getMsg(), Toast.LENGTH_SHORT).show();
                   break;
           }
       }
   };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        client= new OkHttpClient();
    }
    public void setContext(Context ontext) {
        this.mContext=ontext;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_layout, container, false);
        return view;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        //定义RecyclerView管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        //设置RecyclerView布局管理器
        mRecyclerView.setLayoutManager(layoutManager);
        list= new ArrayList<>();
        adapter = new SolarTermAdapter(mContext);
        adapter.setData(list);
        mRecyclerView.setAdapter(adapter);
        //请求接口数据
        initData();


    }

    private void initData() {
        //图片列表接口的地址
        String url = "http://10.111.34.2:8089/home/solarTermList";
        //创建请求
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.i("Fragment1",e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseContent = response.body().string();
                Gson gson = new Gson();
                //由返回的json数据转成一个实体类对象，方便操作调用
                SolarTermResponse solarTermResponse = gson.fromJson(responseContent, SolarTermResponse.class);
               //创建一个Message实例，用于handler传递消息用的
                Message message = Message.obtain();
                if ("00".equals(solarTermResponse.getCode())){//成功
                    message.what=100;
                    message.obj=solarTermResponse;
                }else {//失败
                    message.what=101;
                    message.obj= solarTermResponse;
                }
                handler.sendMessage(message);




            }
        });


    }


}
